package com.foreveredu.propms.persist.hibernate;


import java.util.List;

import com.foreveredu.propms.domain.Building;
import com.foreveredu.propms.domain.Message;
import com.foreveredu.propms.domain.News;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.persist.IMessageDao;


public class HibernateMessageDao extends HibernateAbstractDao<Message, Integer>
		implements IMessageDao {

	private static final long serialVersionUID = 1L;

	public HibernateMessageDao() {
		super(Message.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> findMessageListForTen() {
		List<Message> messages = this.getHibernateTemplate().find("from Message m where m.state=1 order by m.id desc ");
		if (messages != null && messages.size() > 10) {
			return messages.subList(0, 10);
		}
		return messages;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PageModel findMessageList(int offset) {
		List<Message> messages = this.getHibernateTemplate().find("from Message m order by m.id desc");
		PageModel pm = new PageModel();
		pm.setTotal(messages.size());
		if (offset + 10 > messages.size()) {
			pm.setDatas(messages.subList(offset, messages.size()));
			return pm;
		}
		pm.setDatas(messages.subList(offset, offset + 10));
		return pm;
	}

}
